/*
    初始化年份，默认选中当前年份
 */
function initYear(){
    //获取当前年份
    let now = new Date();
    let year = now.getFullYear();

    //遍历选择前后10年
    for (let i = year - 5;i <= year +5;i++){
        //创建子节点option
        let $option = $("<option value="+i+">"+i+"</option>")
        //添加到节点
        $("#year").append($option);
    }

    //过滤，默认选中当前年份
    $("#year option").filter(function (){
        return $(this).val() == year;
    }).prop("selected",true);
}
/*
    初始化月份
 */
function initMonth(){

    //遍历
    for (i = 1;i <= 12 ; i++){
        let month = i < 10?"0"+i:i;
        //创建节点option
        let $option = $("<option value="+month+">"+month+"</option>")

        //把节点option添加到select
        $("#month").append($option);
    }

    let m = new Date();
    //，获取当前年份
    let mon = m.getMonth()+1;
    //默认选中当前月份，
    $("#month option").filter(function (){
        return $(this).val() == mon;
    }).prop("selected",true);
}

/*
    获取某年某月的天数
 */
function getDay(year,month){

    //初始化天数为28
    let day = 28;
     switch (month){
         case 1:
         case 3:
         case 5:
         case 7:
         case 8:
         case 10:
         case 12:
             day = 31;
             break;
         case 4:
         case 6:
         case 9:
         case 11:
             day = 30;
             break;
         case 2:
             if (year % 4==0 || year % 100!=0 && year % 400==0){
                 day = 29;
             }
     }
    return day;
}
/*
    初始化天数
 */
function initDay(year,month){
    //删除原来的日期
    $("#day").find("option").remove();

    var month = parseInt(month);

    var d = getDay(year,month);

    for (i = 1;i <= d;i++){

        let now = i < 10?"0"+i:i;

        //创建节点option
        let $option = $("<option value="+now+">"+now+"</option>")

        //添加到select节点
        $("#day").append($option);
    }
    let day = new Date();
    let days = day.getDate();

    //过滤，默认选中当前日期
    $("#day option").filter(function (){
        return $(this).val() == days;
    }).prop("selected",true);
}

/*
    页面加载事件
 */
$(function (){
    //初始化年份
    initYear();

    //初始化月份
    initMonth();

    //初始化日期
    let year = new Date().getFullYear();
    let month =new Date().getMonth()+1;
    initDay(year,month);

    $.ajax({
        url:"Product.Select",
        async:true,
        type:"Get",
        dataType:"json",
        success:function (data){

            $.each(data,function (index,user){
                //创建节点tr
                let $tr = $("<tr></tr>");
                //创建td节点
                let $td = $("<td><input type='checkbox' class='ck' value='"+user.prodId+"'></td>");
                let $td2 =$("<td>"+user.prodId+"</td>");
                let $td3 =$("<td>"+user.prodName+"</td>");
                let $td4 =$("<td>"+user.prodPrice+"</td>");
                let $td5 =$("<td>"+user.prodNow+"</td>");
                let $td6 =$("<td><input type='checkbox'"+(user.prodIsno==1?"checked":"")+ "/></td>");
                let $td7 =$("<td><span class='dele'>删除</span><span class='upda'>修改</span><span class='' sele'>详情</span></td>");

                //添加td节点到tr节点里
                $tr.append($td).append($td2).append($td3).append($td4).append($td5).append($td6).append($td7);
                //添加tr节点到thead节点里
                $("#tb tbody").append($tr)
            })
        },
        error:function (){
            console.log("服务器响应失败")
        }
    });
})
/*
    单删
 */
$("#tb").on("click",".dele",function (){

    //获取商品id
    let id = $(this).closest("tr").find("td:eq(1)").html();

    //获取点击的行
    let $tr = $(this).closest("tr");

    $.ajax({
        url: "dele.product",
        async: true,
        type: "get",
        data:"id="+id,
        dataType: "text",
        success:function (data){
            $tr.remove();
            alert(data);
        },
        error:function (){
            console.log("error")
        }
    })
})

/*
    全选
 */
$("#allcheck").click(function (){
    let allck = $(this).prop("checked");
    $(".ck").prop("checked",allck)
});

/*
    批量删除
 */
$("#btnDels").click(function (){

    //获取多选框的 选中状态
    let $check = $(".ck:checked");

    //定义空字符串累加 id
    let number = "";

    //遍历
   $check.each(function (index,element){
       number+=element.value+","
   });

    $.get(
        "batchdele",
        "number="+number,
        function (data){//接收数据
            if (data=="删除成功"){
                $check.closest("tr").remove();
            }else {
                alert("删除失败!")
            }
        },
        "text"
    );
});

/*
    添加商品
 */
$("#btnAdd").click(function (){

    //获取表单里的数据
    let id = document.getElementById("productId").value;
    let name = document.getElementById("productName").value;
    let price = document.getElementById("productPrice").value;

    let year = document.getElementById("year").value;
    let month = document.getElementById("month").value;
    let day = document.getElementById("day").value;
    //日期拼接
    let ymd = year+"-"+ month+"-"+day;

    let $box =$("input[name=isDown]:checked").val() == 1?1:0;

    $.ajax({
        url:"add.product",
        async:true,
        type:"Get",
        data: "id="+id+"&name="+name+"&price="+price+"&ymd="+ymd+"&box="+$box,
        dataType:"text",
        success:function (data){
            console.log(data)
        },
        error:function (){
            console.log("服务器响应失败")
        }
    })
});

